package com.ceres.customer.controller;

import com.ceres.customer.pojo.User;
import com.ceres.customer.service.CustUserService;
import com.ceres.entity.Result;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/custUser")
public class CustUserController {

    @Autowired
    private CustUserService custUserService;

    /**
     * 用户修改方法
     *
     * @param id
     * @return
     */
    @GetMapping("/update")
    public Result update(int id) {
        try {
            custUserService.update(id);
            return new Result(true, 2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 分页模糊查询
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(String keyword, int pageNum, int pageSize) {
        try {
            Page findPage = custUserService.findPage(keyword, pageNum, pageSize);
            return new Result(true, 2000, findPage);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 通过id查询农业组织
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id) {
        try {
            User user = custUserService.findById(id);
            return new Result(true, 2000, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 查询所有用户
     * （包括已删除的）
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<User> userList = custUserService.findAll();
            return new Result(true, 2000, userList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }
}
